package csa

import (
	"git.sr.ht/~mil/mobroute/src/db"
)

// Transfers are a two-way operation so a transfer a-b also validates
// b-a. The returned datastructured is indexed as fromstop[tostop]uint
// which is the
// db.Transfer holds the
func buildTransfersMap(transfers *[]db.Transfer) map[string]map[string]uint {
	r := map[string]map[string]uint{}
	for i, _ := range *transfers {
		if _, exists := r[(*transfers)[i].FromSSIDU]; !exists {
			r[(*transfers)[i].FromSSIDU] = map[string]uint{}
		}
		if _, exists := r[(*transfers)[i].ToSSIDU]; !exists {
			r[(*transfers)[i].ToSSIDU] = map[string]uint{}
		}
		r[(*transfers)[i].FromSSIDU][(*transfers)[i].ToSSIDU] = (*transfers)[i].TransferTime
		r[(*transfers)[i].ToSSIDU][(*transfers)[i].FromSSIDU] = (*transfers)[i].TransferTime
	}
	return r
}

func InitializeMapStopToArrivalDetails(initialStopToArrivalTimeMap map[string]uint) map[string]ArrivalDetails {
	r := map[string]ArrivalDetails{}
	for stop, arrivalTime := range initialStopToArrivalTimeMap {
		r[stop] = ArrivalDetails{
			Time:       arrivalTime,
			Connection: nil,
		}
	}
	return r
}

func ConnIDs(conns []*db.Connection) []string {
	r := []string{}
	for _, c := range conns {
		r = append(r, c.ConnID)
	}
	return r
}

func roundUp(value, roundToMultiple uint) uint {
	return (value + roundToMultiple - 1) / roundToMultiple * roundToMultiple
}
